package com.tencent.wxcloudrun.frontcontroller;

import com.alibaba.fastjson.JSONObject;
import com.tencent.wxcloudrun.dto.*;
import com.tencent.wxcloudrun.service.AddressService;
import io.swagger.annotations.Api;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.UUID;

/**
 * @author lichongbing
 * @version 1.0.0
 * @createdate 2023/3/4 19:13
 * @description: TODO
 */
@RestController
@Api(tags = "收货地址模块")
public class AddressController {


    @Autowired
    private AddressService addressService;


    /**
     * 修改收货地址
     */
    @Operation(summary = "修改收货地址")
    @PostMapping("/wxcloud/updateAddress")
    UpdateAddressResponse updateAddress(HttpServletRequest request, @RequestBody Address json) {
      int ok =  addressService.updateAddress(json);
        UpdateAddressResponse response = new UpdateAddressResponse();
        if (ok > 0) {
            response.setCode(0);
            response.setMsg("修改成功");
            return response;
        } else {
            response.setCode(400);
            response.setMsg("修改失败");
            return response;
        }
    }

    @Operation(summary = "删除收货地址")
    @PostMapping("/wxcloud/deleteAddress")
    ResponseBase deleteAddress(HttpServletRequest request, @RequestBody DeleteAddressRequest json) {
        ResponseBase responseBase = new ResponseBase();
        int delet = addressService.deleteAddress(json.getId());
        if (delet > 0) {
            responseBase.setCode(0);
            responseBase.setMsg("删除成功");
            return responseBase;
        } else {
            responseBase.setCode(100);
            responseBase.setMsg("删除失败");
            return responseBase;
        }

    }

    @Operation(summary = "查询收货地址列表")
    @GetMapping("/wxcloud/queryAddress")
    QueryAddressResponse queryAddress(HttpServletRequest request) {
        String openid = request.getHeader("X-WX-OPENID");
        QueryAddressResponse response = new QueryAddressResponse();
        List<Address> addressList = addressService.queryAddress(openid);
        response.setData(addressList);
        return response;

    }

    /**
     * 添加收货地址
     *
     * @param request
     * @param json
     * @return
     */
    @Operation(summary = "添加收货地址")
    @PostMapping("/wxcloud/addAddress")
    Response addAddress(HttpServletRequest request, @RequestBody JSONObject json) {
        String openid = request.getHeader("X-WX-OPENID");
        Address address = new Address();
        address.setId(idGenerator());
        address.setAddress(json.getString("address"));
        address.setLinkman(json.getString("linkman"));
        address.setMobile(json.getString("mobile"));
        address.setProvinceid(json.getInteger("provinceid"));
        address.setIsdefault(json.getInteger("isdefault"));
        address.setCityid(json.getString("cityid"));
        address.setOpenid(openid);
        try {
            int add = addressService.addAddress(address);
            if (add > 0) {
                return new Response();
            } else {
                Response response = new Response();
                response.setCode(100);
                response.setMsg("添加失败");
                return response;
            }
        } catch (Exception e) {
            e.printStackTrace();
            Response response = new Response();
            response.setCode(100);
            response.setMsg("添加失败");
            return response;

        }
    }


    //id工具生成
    private static String idGenerator() {
        return UUID.randomUUID().toString().replace("-", "");
    }


    /**
     * 收货地址详情
     */
    @Operation(summary = "收货地址详情")
    @PostMapping("/wxcloud/addressDetail")
    AddressDetailResponse addressDetail(HttpServletRequest request, @RequestBody AddressDetailRequest json) {
    Address  address =  addressService.addressDetail(json.getId());
        AddressDetailResponse response = new AddressDetailResponse();
        AddressData addressData = new AddressData();
        addressData.setInfo(address);
        response.setData(addressData);
        return response;
    }

    @Operation(summary = "获取默认地址")
    @GetMapping("/wxcloud/defaultAddress")
    DefaultAddressResponse defaultAddress(HttpServletRequest request) {
        String openid = request.getHeader("X-WX-OPENID");
        Address  address =  addressService.defaultAddress(openid);
        DefaultAddressResponse response = new DefaultAddressResponse();
       // response.setData(address);
        return null;
    }

    @Operation(summary = "计算距离")
    @PostMapping("/wxcloud/gpsDistance")
    GpsDistanceResponse gpsDistance(HttpServletRequest request, @RequestBody GpsDistanceRequest json) {
        return null;
    }


}
